package com.yyplatform.admin.platform.config.controller;

import static com.yyplatform.admin.Constants.URL_PREFIX;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.yyplatform.admin.infra.model.RestResponse;
import com.yyplatform.admin.platform.config.model.dto.SysConfigDTO;
import com.yyplatform.admin.platform.config.model.dto.SysConfigFormDTO;
import com.yyplatform.admin.platform.config.model.dto.SysConfigQueryDTO;
import com.yyplatform.admin.platform.config.service.SysConfigService;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * Created on 2024-09-18
 */
@Slf4j
@Tag(name = "配置/配置管理")
@AllArgsConstructor
@RestController
@RequestMapping(URL_PREFIX + "/sys/config/")
public class SysConfigController {

    private SysConfigService sysConfigService;


    @GetMapping("query")
    @Operation(summary = "查询")
    @RequiresPermissions("sys:config:query")
    public RestResponse<Page<SysConfigDTO>> page(SysConfigQueryDTO queryDTO) {
        log.debug("query dto = {}", queryDTO);

        Page<SysConfigDTO> page = sysConfigService.query(queryDTO);

        return RestResponse.ok(page);
    }


    @GetMapping("get")
    @Operation(summary = "通过ID获取详情")
    @RequiresPermissions("sys:config:get")
    public RestResponse<SysConfigDTO> get(Long id) {
        log.debug("delete id :{}", id);

        SysConfigDTO data = sysConfigService.get(id);
        return RestResponse.ok(data);
    }


    @PostMapping("save")
    @Operation(summary = "保存")
    @RequiresPermissions("sys:config:save")
    public RestResponse<Void> save(@RequestBody SysConfigFormDTO sysConfigFormDTO) {
        log.debug("save dto = {}", sysConfigFormDTO);

        sysConfigService.save(sysConfigFormDTO);

        return RestResponse.ok(null);
    }


    @PostMapping("update")
    @Operation(summary = "修改")
    @RequiresPermissions("sys:config:update")
    public RestResponse<Void> update(@RequestBody SysConfigFormDTO sysConfigFormDTO) {
        log.debug("update dto = {}", sysConfigFormDTO);

        sysConfigService.update(sysConfigFormDTO);
        return RestResponse.ok(null);
    }


    @PostMapping("delete")
    @Operation(summary = "删除")
    @RequiresPermissions("sys:config:delete")
    public RestResponse<Void> delete(Long id) {
        log.debug("delete id is  {}", id);

        sysConfigService.delete(id);
        return RestResponse.ok(null);
    }


}
